package com.guzhen.wallpager;

import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.View.MeasureSpec;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup;

public final class SizeUtils {
    public interface OnGetSizeListener {
        void onGetSize(View arg1);
    }

    private SizeUtils() {
        throw new UnsupportedOperationException("u can\'t instantiate me...");
    }

    public static float applyDimension(float f, int v) {
        DisplayMetrics displayMetrics0 = Resources.getSystem().getDisplayMetrics();
        switch(v) {
            case 0: {
                return f;
            }
            case 1: {
                return f * displayMetrics0.density;
            }
            case 2: {
                return f * displayMetrics0.scaledDensity;
            }
            case 3: {
                return f * displayMetrics0.xdpi * 0.013889f;
            }
            case 4: {
                return f * displayMetrics0.xdpi;
            }
            case 5: {
                return f * displayMetrics0.xdpi * 0.03937f;
            }
            default: {
                return 0.0f;
            }
        }
    }

    public static int dp2px(float f) {
        return (int)(f * Resources.getSystem().getDisplayMetrics().density + 0.5f);
    }

    public static void forceGetViewSize(View view0, OnGetSizeListener sizeUtils$OnGetSizeListener0) {
        view0.post(new Runnable() {
            @Override
            public void run() {
                if(sizeUtils$OnGetSizeListener0 != null) {
                    sizeUtils$OnGetSizeListener0.onGetSize(view0);
                }
            }
        });
    }

    public static int getMeasuredHeight(View view0) {
        return SizeUtils.measureView(view0)[1];
    }

    public static int getMeasuredWidth(View view0) {
        return SizeUtils.measureView(view0)[0];
    }

    public static int[] measureView(View view0) {
        ViewGroup.LayoutParams viewGroup$LayoutParams0 = view0.getLayoutParams();
        if(viewGroup$LayoutParams0 == null) {
            viewGroup$LayoutParams0 = new ViewGroup.LayoutParams(-1, -2);
        }

        view0.measure(ViewGroup.getChildMeasureSpec(0, 0, viewGroup$LayoutParams0.width), (viewGroup$LayoutParams0.height <= 0 ? 0 : View.MeasureSpec.makeMeasureSpec(viewGroup$LayoutParams0.height, 0x40000000)));
        return new int[]{view0.getMeasuredWidth(), view0.getMeasuredHeight()};
    }

    public static int px2dp(float f) {
        return (int)(f / Resources.getSystem().getDisplayMetrics().density + 0.5f);
    }

    public static int px2sp(float f) {
        return (int)(f / Resources.getSystem().getDisplayMetrics().scaledDensity + 0.5f);
    }

    public static int sp2px(float f) {
        return (int)(f * Resources.getSystem().getDisplayMetrics().scaledDensity + 0.5f);
    }
}

